<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>pytorch实现手写数字识别 | 杨一赫的博客</title><meta name="author" content="杨一赫"><meta name="copyright" content="杨一赫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="chapter1,torch 入门 创建张量 原有数组转化 Tensor 创建新的 torch.empty()          ones()                    zeros()                    rand()                    randint()                    randn() import">
<meta property="og:type" content="article">
<meta property="og:title" content="pytorch实现手写数字识别">
<meta property="og:url" content="http://yang1he.gitee.io/2022/10/05/pytorch%E5%AE%9E%E7%8E%B0%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB/index.html">
<meta property="og:site_name" content="杨一赫的博客">
<meta property="og:description" content="chapter1,torch 入门 创建张量 原有数组转化 Tensor 创建新的 torch.empty()          ones()                    zeros()                    rand()                    randint()                    randn() import">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yang1he.gitee.io/img/rick1.jpg">
<meta property="article:published_time" content="2022-10-04T16:00:00.000Z">
<meta property="article:modified_time" content="2023-02-23T08:21:41.467Z">
<meta property="article:author" content="杨一赫">
<meta property="article:tag" content="-动手学深度学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yang1he.gitee.io/img/rick1.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://yang1he.gitee.io/2022/10/05/pytorch%E5%AE%9E%E7%8E%B0%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?6b9e2c1c603a1d11d43a9822bdd68c2c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":90,"position":"top","messagePrev":"距离上次更新已经","messageNext":"天本文可能会过期"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'pytorch实现手写数字识别',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-02-23 16:21:41'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="https://cdn.jsdelivr.net/npm/echarts@5.3.0/dist/echarts.min.js"></script><script src="https://cdn.jsdelivr.net/npm/echarts@4.7.0/map/js/china.min.js"></script><script src="node_modules/simplex-noise/simplex-noise.js"></script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="杨一赫的博客" type="application/atom+xml">
</head><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js/themes/blue/pace-theme-minimal.min.css"/><script src="https://cdn.jsdelivr.net/npm/pace-js/pace.min.js"></script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon2.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/rick1.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">杨一赫的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">pytorch实现手写数字识别</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-10-04T16:00:00.000Z" title="发表于 2022-10-05 00:00:00">2022-10-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-02-23T08:21:41.467Z" title="更新于 2023-02-23 16:21:41">2023-02-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E5%85%A5%E9%97%A8/">入门</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="pytorch实现手写数字识别"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="chapter1torch-入门">chapter1,torch 入门</h2>
<h3 id="创建张量">创建张量</h3>
<p>原有数组转化 Tensor</p>
<p>创建新的 torch.empty()</p>
<pre><code>         ones()
         
         zeros()
         
         rand()
         
         randint()
         
         randn()</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> numpy</span><br></pre></td></tr></table></figure>
<p><span class="math inline">\(小写的tensor和Tensor有什么区别\)</span> +
小写的tensor只接受现有的数据； +
而大写的Tensor相当于就是FloatTensor，<mark>既可以接收现有的数据，也可以接受shape来创建指定形状的Tensor。</mark>
+
为了避免混淆，建议接收现有数据的时候使用tensor,指定shape的时候使用Tensor。</p>
<p><strong>因此，tensor支持可选参数dtype,Tensor相当于默认浮点型，所以不支持可选参数Dtype</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#EG1</span></span><br><span class="line">a = torch.tensor([<span class="number">2</span>,<span class="number">3</span>]) <span class="comment">#接受现有的数据</span></span><br><span class="line">a<span class="comment">#tensor([2, 3])</span></span><br><span class="line">b = torch.Tensor([<span class="number">2</span>, <span class="number">3</span>]) <span class="comment">#等价于a = torch.FloatTensor([2, 3.3])</span></span><br><span class="line">b<span class="comment">#tensor([2., 3.])</span></span><br></pre></td></tr></table></figure>
<pre><code>tensor([2, 3])</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">t1=torch.Tensor([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line">t1</span><br></pre></td></tr></table></figure>
<pre><code>tensor([1., 2., 3.])</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">array1=np.arange(<span class="number">12</span>).reshape(<span class="number">3</span>,<span class="number">4</span>)</span><br><span class="line">array1</span><br></pre></td></tr></table></figure>
<pre><code>array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">torch.Tensor(array1)</span><br></pre></td></tr></table></figure>
<pre><code>tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]])</code></pre>
<h3 id="常用方法">常用方法</h3>
<ul>
<li>tensor只有一个元素可用 用 <code>tensor.item()</code> 来取</li>
<li>tensor有多个元素，只获取数据用tensor.data,这样不获取额外属性</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">t1=torch.Tensor([[[<span class="number">1</span>]]])</span><br><span class="line">t1.item()</span><br><span class="line">t1.data</span><br></pre></td></tr></table></figure>
<pre><code>tensor([[[1.]]])</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#用numpy转回</span></span><br><span class="line">t2=torch.Tensor([[<span class="number">1</span>,<span class="number">2</span>]])</span><br><span class="line">t2.numpy()</span><br></pre></td></tr></table></figure>
<pre><code>array([[1., 2.]], dtype=float32)</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#size()获取形状</span></span><br><span class="line"><span class="comment">#括号内可选参数，传入获取哪一层的shape</span></span><br><span class="line">t2.size() </span><br><span class="line">t2.size(<span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<pre><code>2</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#view 改变形状，但不是转置，只是依照新形状进行重新排列</span></span><br><span class="line"><span class="comment">#可以传入形状list,list中值为·-1·代表依照其他参数，以及总size确定形状</span></span><br><span class="line">x=torch.Tensor([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">10</span>]).view([<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line">x</span><br></pre></td></tr></table></figure>
<pre><code>tensor([[ 1.,  2.,  3.],
        [ 4.,  5., 10.]])</code></pre>
<p>矩阵转置 - 二维矩阵转置 tensor.T - 高维矩阵转置
<code>t = torch.rand(3, 4, 5, 6) t_t = torch.transpose(t, 3, 2)  # 想要哪两个维度置换后面的两个整数就是那两个维度</code>
- 也可以直接在tensor后面根transpose方法
<code>t.transpose(t,3,2)</code></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">t = torch.rand(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">t_t = torch.transpose(t, <span class="number">2</span>, <span class="number">1</span>)  <span class="comment"># 想要哪两个维度置换后面的两个整数就是那两个维度,从0开始数</span></span><br><span class="line">t_t</span><br></pre></td></tr></table></figure>
<pre><code>tensor([[[0.2870, 0.0155],
         [0.6913, 0.4358],
         [0.4485, 0.7475]]])</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#获取阶数</span></span><br><span class="line">tensor.dim()</span><br><span class="line"><span class="comment">#获取最大值最小值</span></span><br><span class="line">tensor.<span class="built_in">max</span>()</span><br><span class="line">tensor.<span class="built_in">min</span>()</span><br><span class="line"><span class="comment">#获取标准差</span></span><br><span class="line">tensor.std()</span><br><span class="line"><span class="comment">#查看他的类型</span></span><br><span class="line">tensor.dtype()</span><br></pre></td></tr></table></figure>
<p>取值、赋值操作</p>
<ul>
<li>与列表类似方法切片，eg: <code>tensor[0,:,:]</code></li>
<li>切片后可以赋值 <code>tensor[0,0,0]=100</code></li>
</ul>
<h3 id="数据类型">数据类型</h3>
<table>
<colgroup>
<col style="width: 27%" />
<col style="width: 72%" />
</colgroup>
<thead>
<tr class="header">
<th>Data Type</th>
<th>dtype</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>32 bit floating point</td>
<td><code>torch.float32</code> <code>torch.float</code></td>
</tr>
<tr class="even">
<td>64 bit floating point</td>
<td><code>torch.double</code> <code>torch.float64</code></td>
</tr>
<tr class="odd">
<td>16 bit floating point</td>
<td><code>torch.float16</code> <code>torch.half</code></td>
</tr>
<tr class="even">
<td>8 bit integer(unsigned)</td>
<td><code>torch.uint8</code></td>
</tr>
<tr class="odd">
<td>8 bit integer(signed)</td>
<td><code>torch.int8</code></td>
</tr>
<tr class="even">
<td>......</td>
<td>torch.int16 or torch.short<br />torch.int32 or
torch.int<br />torch.int64 or torch.long</td>
</tr>
</tbody>
</table>
<ul>
<li>一是创建时候设置好他的类型 ，eg</li>
</ul>
<p><code>t5=torch.tensor(np.array([1,0]),dtype=torch.float)</code> +
二是后来修改她的类型 后来修改有两种方法 + 法一 + tensor.int() +
tensor.float() + tensor.long() + tensor.double() + 法二 +
tensor.type(torch.float32) + tensor.type(torch.int)</p>
<h3 id="tensor的其他操作">tensor的其他操作</h3>
<p><strong>相加</strong> + tensor.add(x,y) + x+y + x.add_(y)<br />
<span
class="math display">\[带下划线就地修改，直接修改x的内容\]</span></p>
<p><strong>torch 中的cuda</strong></p>
<p>torch.cuda.is.available() #判断是否支持cuda</p>
<p>device=torch.device("cuda:0" if torch.cuda.is.available() else "cpu"
) #这样很方便 if torch.cuda.is_available(): device=torch.device('cuda')
y=torch.ones_like(x,device=device) x=x.to(device) z=x+y print(z)
print(z.to("cpu",torch.double) torch.cuda.is_available():</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">device=torch.device(<span class="string">&#x27;cpu&#x27;</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">device</span><br></pre></td></tr></table></figure>
<pre><code>device(type=&#39;cpu&#39;)</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">torch.zeros([<span class="number">2</span>,<span class="number">3</span>],device=device)</span><br></pre></td></tr></table></figure>
<pre><code>tensor([[0., 0., 0.],
        [0., 0., 0.]])</code></pre>
<h2 id="chapter2-梯度下降与反向传播">chapter2 梯度下降与反向传播</h2>
<p>梯度：是一个向量，导数＋变化最快的方向（<mark>参数学习的前进方向</mark>）
判断模型好坏的方法</p>
<p><span
class="math display">\[loss=(Y_{predict}-Y_{true})^2    回归损失   \]</span>
<span
class="math display">\[loss=Y_{true}*log(predict)     分类损失\]</span>
·Markdown上标和下标如果上下标只对其后面的一个字符起作用如果上下标的内容超过一个字符则需要用花括号包裹否则上下标只对后面的一个符号起作用
·</p>
<p><strong>目标：通过调整参数W，尽可能降低Loss</strong></p>
<p>requires_grad=True ,能进行梯度计算 ，默认为FALSE +
grad_fn=True，该tensor所有的后续操作都会记录在grad_fn里面 +
默认为null，若想做梯度下降之类的，就得设定成True</p>
<p>防止跟踪历史记录，可以将代码块包装在下面这个模块中，因为模型可能具有requires_grad=True
的可训练参数，但是我们不需要对其进行梯度计算 <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">with torch.no_gard():</span><br><span class="line">    c=(a*a).sum()  #此时c没有grad_fn</span><br></pre></td></tr></table></figure> 可以<span
class="burk">加杠就地修改</span></p>
<p><mark>a.requires_grad_(True)</mark></p>
<ul>
<li>获取梯度x.grad,累加梯度</li>
<li>反向传播： output.backward（）</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line">x=torch.ones(<span class="number">2</span>,<span class="number">2</span>,requires_grad=<span class="literal">True</span>)</span><br><span class="line">y=x+<span class="number">2</span></span><br><span class="line">z=y*y*<span class="number">3</span></span><br><span class="line">out=z.mean()</span><br><span class="line">out</span><br></pre></td></tr></table></figure>
<pre><code>tensor(27., grad_fn=&lt;MeanBackward0&gt;)</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">a=torch.randn(<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line">a=((a*<span class="number">3</span>)/(a-<span class="number">1</span>))</span><br><span class="line">a.requires_grad</span><br><span class="line"><span class="comment">#False</span></span><br><span class="line">a.requires_grad_(<span class="literal">True</span>) <span class="comment">#就地修改</span></span><br><span class="line">a.requires_grad</span><br></pre></td></tr></table></figure>
<pre><code>True</code></pre>
<p>tensor有requires_fn=True时，不能直接转化为NUmpy,要detach（）掉属性，才能numpy</p>
<p>tensor.detach().numpy()</p>
<h2 id="线性回归的实现">线性回归的实现</h2>
<h3 id="手敲">手敲</h3>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1.准备数据</span></span><br><span class="line"><span class="comment"># y=3x+0.8</span></span><br><span class="line">x = torch.rand(<span class="number">500</span>,<span class="number">1</span>)</span><br><span class="line">y_true = x*<span class="number">0.3</span> + <span class="number">0.8</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.通过模型计算y_predict</span></span><br><span class="line">w = torch.rand(<span class="number">1</span>, requires_grad=<span class="literal">True</span>)  <span class="comment"># 初始化参数</span></span><br><span class="line">b = torch.tensor(<span class="number">0</span>, requires_grad=<span class="literal">True</span>, dtype=torch.float32)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 4.通过循环，反向传播，更新参数</span></span><br><span class="line">learning_rate = <span class="number">1e-3</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3000</span>):</span><br><span class="line">    <span class="comment"># 3.计算loss</span></span><br><span class="line">    y_predict = torch.matmul(x, w) + b</span><br><span class="line">    loss = (y_true - y_predict).<span class="built_in">pow</span>(<span class="number">2</span>).mean()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 每次循环前判断梯度是否为0，如果不为0，则置为0</span></span><br><span class="line">    <span class="keyword">if</span> w.grad <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">        w.grad.data.zero_()</span><br><span class="line">    <span class="keyword">if</span> b.grad <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">        b.grad.data.zero_()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 反向传播</span></span><br><span class="line">    loss.backward()</span><br><span class="line">    w.data -= learning_rate*w.grad</span><br><span class="line">    b.data -= learning_rate*b.grad</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;w, b, loss&quot;</span>,w.item(), b.item(), loss.i/tem())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">plt.figure()</span><br><span class="line">plt.scatter(x.numpy().reshape(-<span class="number">1</span>), y_true.numpy().reshape(-<span class="number">1</span>))</span><br><span class="line">y_predict = torch.matmul(x, w) + b</span><br><span class="line">plt.plot(x.numpy().reshape(-<span class="number">1</span>), y_predict.detach().numpy().reshape(-<span class="number">1</span>), c=<span class="string">&#x27;r&#x27;</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/output_37_1.png"
alt="png" />
<figcaption aria-hidden="true">png</figcaption>
</figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#方法二，未完成</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"><span class="comment">#准备数据</span></span><br><span class="line"><span class="comment"># y=3x+0.8</span></span><br><span class="line">x = torch.rand(<span class="number">500</span>,<span class="number">1</span>)</span><br><span class="line">y = x*<span class="number">0.3</span> + <span class="number">0.8</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#初始化参数</span></span><br><span class="line">w = torch.rand(<span class="number">1</span>, requires_grad=<span class="literal">True</span>)  <span class="comment"># 初始化参数</span></span><br><span class="line">b = torch.tensor(<span class="number">0</span>, requires_grad=<span class="literal">True</span>, dtype=torch.float32)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">loss_fn</span>(<span class="params">y,y_predict</span>):</span><br><span class="line">    loss=(y_predict-y),<span class="built_in">pow</span>(<span class="number">2</span>).mean()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> [w,b]:</span><br><span class="line">        <span class="keyword">if</span> i.grad <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">            i.grad.data.zero_()</span><br><span class="line">    loss.backward()</span><br><span class="line">    <span class="keyword">return</span> loss.data</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">optimize</span>(<span class="params">learning_rate</span>):</span><br><span class="line">    w.data-=learning_rate*w.grad</span><br><span class="line">    b.data-=learning_rate*b.grad</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3000</span>):</span><br><span class="line">    y_predict=x*w+b</span><br><span class="line">    loss =loss_fn(y,y_predict)</span><br><span class="line"></span><br><span class="line">plt.figure()</span><br><span class="line">plt.scatter(x.numpy().reshape(-<span class="number">1</span>), y_true.numpy().reshape(-<span class="number">1</span>))</span><br><span class="line">y_predict = torch.matmul(x, w) + b</span><br><span class="line">plt.plot(x.numpy().reshape(-<span class="number">1</span>), y_predict.detach().numpy().reshape(-<span class="number">1</span>), c=<span class="string">&#x27;r&#x27;</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<h3 id="借助api">借助api</h3>
<p>nn.Module 是torch.nn提供的一个类，有两点注意 +
__init__需要super方法，继承父类的属性和方法 +
farward方法必须实现，用来定义我们的网络的向前计算的过程</p>
<p>+nn,Linear(,)是一个预先设定好的线性模型，也被称为全连接层，传入参数为，输入的数量，输出的数量</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> optim</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Lr</span>(nn.Module):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>(Lr,self).__init__() <span class="comment">#继承父类</span></span><br><span class="line">        self.linear=nn.Linear(<span class="number">1</span>,<span class="number">1</span>) <span class="comment">#设定自己的操作，</span></span><br><span class="line">        </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self,x</span>):</span><br><span class="line">        out=self.linear(x)</span><br><span class="line">        <span class="keyword">return</span> out</span><br><span class="line">    </span><br><span class="line">model=Lr()</span><br><span class="line"><span class="comment">#nn.Module定义了__call__方法，实现调用forward方法，即Lr的实例，能够直接被传入的参数调用，实际上调用的是forward方法并传入参数</span></span><br><span class="line">predict=model(x)</span><br></pre></td></tr></table></figure>
<p>优化器类 torch封装的用来进行更新参数的方法，比如常见的随机梯度下降
优化器类都是由torch.optim提供的，例如 + torch.optim.SGD(参数，学习率) +
torch.optim.Adam(参数，学习率)</p>
<p>注意 +
参数可以由model.parameters()来获取，获取模型中所有requires_grad=True
的参数 + 优化类的使用方法 + 1，实例化 + 2， 所有参数的梯度，重置为0 +
3，反向传播计算梯度 + 4，更新参数值</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">optimizer=optim.SGD(model.parameters(),lr=<span class="number">1e-3</span>) <span class="comment">#实例化</span></span><br><span class="line">optimizer.zero_gard()  <span class="comment">#重置梯度</span></span><br><span class="line">loss.backward()  <span class="comment">#计算梯度</span></span><br><span class="line">optimizer.step() <span class="comment">#更新参数</span></span><br></pre></td></tr></table></figure>
<pre><code>---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

Input In [29], in &lt;cell line: 2&gt;()
      1 optimizer=optim.SGD(model.parameters(),lr=1e-3) #实例化
----&gt; 2 optimizer.zero_gard()  #重置梯度
      3 loss.backward()  #计算梯度
      4 optimizer.step()


AttributeError: &#39;SGD&#39; object has no attribute &#39;zero_gard&#39;</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">model=Lr()</span><br><span class="line"><span class="comment">#nn.Module定义了__call__方法，实现调用forward方法，即Lr的实例，能够直接被传入的参数调用，实际上调用的是forward方法并传入参数</span></span><br><span class="line">predict=model(x)</span><br><span class="line">criterion=nn.MSELoss()</span><br><span class="line">optimizer=optim.SGD(model.parameters(),lr=<span class="number">1e-3</span>) <span class="comment">#实例化</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1000</span>):</span><br><span class="line">    y_predict=model(x_true)</span><br><span class="line">    loss=criterion(y_truerue,y_predict)<span class="comment">#传入真实值和预测值得到损失函数</span></span><br><span class="line">    optimizer.zero_gard()  <span class="comment">#重置梯度</span></span><br><span class="line">    loss.backward()  <span class="comment">#计算梯度</span></span><br><span class="line">    optimizer.step() <span class="comment">#更新参数</span></span><br></pre></td></tr></table></figure>
<pre><code>---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

Input In [28], in &lt;cell line: 6&gt;()
      5 optimizer=optim.SGD(model.parameters(),lr=1e-3) #实例化
      6 for i in range(1000):
----&gt; 7     y_predict=model(x_true)
      8     loss=criterion(y_truerue,y_predict)#传入真实值和预测值得到损失函数
      9     optimizer.zero_gard()  #重置梯度


NameError: name &#39;x_true&#39; is not defined</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#合并成整体</span></span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> optim</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line">x = torch.rand(<span class="number">5000</span>,<span class="number">1</span>)</span><br><span class="line">y = <span class="number">3</span>*x + <span class="number">0.8</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Lr</span>(nn.Module):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__() <span class="comment">#继承父类</span></span><br><span class="line">        self.linear=nn.Linear(<span class="number">1</span>,<span class="number">1</span>) <span class="comment">#设定自己的操作，</span></span><br><span class="line">        </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self,x</span>):</span><br><span class="line">        out=self.linear(x)</span><br><span class="line">        <span class="keyword">return</span> out</span><br><span class="line"></span><br><span class="line"><span class="comment">#实例化模型，优化器，Loss实例</span></span><br><span class="line">model=Lr()</span><br><span class="line">loss_fn=nn.MSELoss()</span><br><span class="line">optimizer=optim.SGD(model.parameters(),lr=<span class="number">1e-3</span>) <span class="comment">#实例化</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#循环进行梯度下降，参数的更新</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10000</span>):</span><br><span class="line">    y_predict=model(x)</span><br><span class="line">    loss=loss_fn(y,y_predict)<span class="comment">#传入真实值和预测值得到损失函数</span></span><br><span class="line">    optimizer.zero_grad()  <span class="comment">#重置梯度</span></span><br><span class="line">    loss.backward()  <span class="comment">#计算梯度</span></span><br><span class="line">    optimizer.step() <span class="comment">#更新参数</span></span><br><span class="line">    <span class="keyword">if</span> i%<span class="number">50</span>==<span class="number">0</span>:</span><br><span class="line">        param=<span class="built_in">list</span>(model.parameters())</span><br><span class="line">        <span class="built_in">print</span>(loss.item(),param[<span class="number">0</span>].item(),param[<span class="number">1</span>].item())</span><br><span class="line"></span><br><span class="line">model.<span class="built_in">eval</span>()  <span class="comment">#设置模型进入评估模式 或者model.train(model=True)</span></span><br><span class="line">plt.scatter(x.data.numpy(), y.data.numpy(), c=<span class="string">&#x27;r&#x27;</span>)</span><br><span class="line">plt.plot(x.data.numpy(),y_predict.data.numpy())</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/output_45_1.png"
alt="png" />
<figcaption aria-hidden="true">png</figcaption>
</figure>
<h3 id="在gpu上运行代码">在GPu上运行代码</h3>
<p>注意 + 参数和数据，需要转化为cuda支持的tensor + model
需要转化为cuda支持的model + 执行结果需要和cpu的tensor计算的时候 +
tensor.cpu()把cuda的tensor转化为CPU的tensor</p>
<p>predict.cpu().detach().numpy()</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#整体运行</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> optim</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个device对象</span></span><br><span class="line">device = torch.device(<span class="string">&#x27;cuda&#x27;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&#x27;cpu&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&#x27;Using <span class="subst">&#123;device&#125;</span>!&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 1.准备数据</span></span><br><span class="line">x = torch.rand(<span class="number">500</span>,<span class="number">1</span>)</span><br><span class="line">y = <span class="number">0.3</span>*x + <span class="number">0.8</span></span><br><span class="line">x, y = x.to(device), y.to(device)  <span class="comment"># 将训练数据分配给device</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.定义模型</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">myLr</span>(nn.Module):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>(myLr, self).__init__()</span><br><span class="line">        self.Linear = nn.Linear(<span class="number">1</span>, <span class="number">1</span>)   <span class="comment"># nn.Linear(输入的特征数, 输出的特征数)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 自定义模型必须实现forward方法</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self, x</span>):</span><br><span class="line">        out = self.Linear(x)</span><br><span class="line">        <span class="keyword">return</span> out</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3.实例化模型，优化器类实例化，loss实例化</span></span><br><span class="line">my_linear = myLr().to(device)  <span class="comment"># 实例化模型, 将模型分配给device(只需要给模型分配即可)</span></span><br><span class="line">optimizer = optim.SGD(my_linear.parameters(), lr=<span class="number">1e-3</span>)  <span class="comment"># 实例化优化器</span></span><br><span class="line">loss_fn = nn.MSELoss()  <span class="comment"># 实例化损失函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 4.循环，进行梯度下降，参数更新</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3000</span>):</span><br><span class="line">    <span class="comment"># 得到预测值</span></span><br><span class="line">    y_predict = my_linear(x)</span><br><span class="line">    loss = loss_fn(y_predict, y)  <span class="comment"># 注意第一个参数为预测值，第二个参数为真实值</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 梯度置为0</span></span><br><span class="line">    optimizer.zero_grad()</span><br><span class="line">    <span class="comment"># 反向传播，计算梯度</span></span><br><span class="line">    loss.backward()</span><br><span class="line">    <span class="comment"># 参数更新</span></span><br><span class="line">    optimizer.step()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> i%<span class="number">100</span>==<span class="number">0</span>:</span><br><span class="line">        params = <span class="built_in">list</span>(my_linear.parameters())  <span class="comment"># 取出模型中的参数，进行显示</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;params:&#x27;</span>,params)</span><br><span class="line">        <span class="built_in">print</span>(loss.item(), params[<span class="number">0</span>].item(), params[<span class="number">1</span>].item())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">my_linear.<span class="built_in">eval</span>() <span class="comment"># 设置评估模式</span></span><br><span class="line">predict = my_linear(x)</span><br><span class="line">plt.scatter(x.cpu().data.numpy(), y.cpu().data.numpy(), c=<span class="string">&#x27;r&#x27;</span>)</span><br><span class="line">plt.plot(x.cpu().data.numpy(), predict.cpu().data.numpy())</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/output_49_1.png"
alt="png" />
<figcaption aria-hidden="true">png</figcaption>
</figure>
<h3 id="常见优化算法介绍">常见优化算法介绍</h3>
<ul>
<li>梯度下降算法 BGD 全局最优</li>
<li>随机梯度下降 SGD 随机从样本中抽出一个样本进行梯度更新</li>
<li>小批量梯度下降算法 MBGD 一波数据计算梯度，使用均值更新参数</li>
<li>动量法
使用梯度的移动指数加权平均，用前几次梯度加权平均做为当前梯度</li>
<li>AdaGrad 自适应学习率</li>
<li>Rmsprop 对动量法的优化 对学习率加权</li>
<li>Adam 动量法+Rmsprop 学习率自适应，梯度振幅不会过大</li>
</ul>
<h2 id="数据加载">数据加载</h2>
<h3 id="dataset介绍">DATASET介绍</h3>
<p>torch.utils.data.Dataset. 继承这个基类，非常快对数据实现加载 +
batch_size 传入数据的batch大小，常用128,一批传入几个 +
如果最后一批凑不够数，可调用drop_last=True 使之不报错，直接删掉尾数</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> optim</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> Dataset, DataLoader</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line">data_path = <span class="string">r&#x27;./data/smsspamcollection/SMSSpamCollection&#x27;</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyDataset</span>(<span class="title class_ inherited__">Dataset</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        self.lines= <span class="built_in">open</span>(data_path, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>).readlines()<span class="comment">#每一行作为列表的一个元素</span></span><br><span class="line">        </span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__getitem__</span>(<span class="params">self, item</span>):</span><br><span class="line">        <span class="comment"># 获取索引对应位置的一条数据, item即为索引</span></span><br><span class="line">        line = self.lines[item].strip()  <span class="comment"># 获取索引对应的一行数据 strip()去掉左右两边空格、换行符</span></span><br><span class="line">        label = line[:<span class="number">4</span>].strip() <span class="comment"># 获取数据的标签</span></span><br><span class="line">        content = line[<span class="number">4</span>:].strip() <span class="comment"># 获取数据的内容</span></span><br><span class="line">        <span class="keyword">return</span> label,content</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__len__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="comment"># 返回数据的总数量</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">len</span>(self.lines)</span><br><span class="line">mydataset=MyDataset()</span><br><span class="line">data_loader=DataLoader(dataset=mydataset,batch_size=<span class="number">2</span>,shuffle=<span class="literal">True</span>,num_workers=<span class="number">0</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> data_loader:</span><br><span class="line">    <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure>
<pre><code>[(&#39;ham&#39;, &#39;ham&#39;), (&#39;* Will have two more cartons off u and is very pleased with shelves&#39;, &#39;Hi, can i please get a  &amp;lt;#&amp;gt;  dollar loan from you. I.ll pay you back by mid february. Pls.&#39;)]</code></pre>
<h3 id="dataloader类">DATALOADER类</h3>
<p>数据加载器类 --功能 + 批处理数据 + 打乱数据 +
使用多线程并行加载数据</p>
<p><code>torch.utils.data import  DataLoader</code>提供上诉方法</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data_loader=DataLoader(dataset=mydataset,batch_size=<span class="number">2</span>,shuffle=<span class="literal">True</span>,num_workers=<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> index,(label,content) <span class="keyword">in</span> <span class="built_in">enumerate</span>(data_loader):</span><br><span class="line">    <span class="built_in">print</span>(index,label,content)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data_loader=DataLoader(dataset=mydataset,batch_size=<span class="number">2</span>,shuffle=<span class="literal">True</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> data_loader:</span><br><span class="line">    <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure>
<h3 id="pytorch自带数据集">pytorch自带数据集</h3>
<p>torchvision.datasets + MNIST(手写数字数据)</p>
<p>torchtext.datasets + IMDB(电影评论数据）</p>
<h2 id="进行手写数字识别">进行手写数字识别</h2>
<h3 id="image处理">image处理</h3>
<p>准备数据集的方法前面已经讲过，但是通过前面的内容可知，调用MNIST返回的结果中图形数据是一个Image对象,需要对其进行处理</p>
<p>为了进行数据的处理，接下来学习torchvision.transfroms的方法</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#下载数据集</span></span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">from</span> torchvision.datasets <span class="keyword">import</span> MNIST</span><br><span class="line">mnist=MNIST(root=<span class="string">&quot;./data&quot;</span>,train=<span class="literal">True</span>,download=<span class="literal">False</span>)</span><br><span class="line"><span class="built_in">print</span>(mnist)</span><br></pre></td></tr></table></figure>
<pre><code>Dataset MNIST
    Number of datapoints: 60000
    Root location: ./data
    Split: Train</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#看一下第一个对象长什么样子</span></span><br><span class="line"><span class="built_in">print</span>(mnist[<span class="number">0</span>][<span class="number">0</span>].show())</span><br></pre></td></tr></table></figure>
<pre><code>None</code></pre>
<p>思路 + 准备数据 + 训练 + 保存模型 + 评估</p>
<h4
id="torchvision.transforms的图形数据处理方法">torchvision.transforms的图形数据处理方法</h4>
<p><code>torchvision.transforms.ToTensor</code></p>
<p>把一个取值范围是[0,255]的PIL.Image或者shape为(H,W,C)的numpy.ndarray，转换成形状为[C,H,W]
就是轴变换，把第三转到第一</p>
<p>其中(H,W,C)意思为(高，宽，通道数)，黑白图片的通道数只有1，其中每个像素点的取值为[0,255],</p>
<p>彩色图片的通道数为(R,G,B),每个通道的每个像素点的取值为[0,255]，三个通道的颜色相互叠加，形成了各种颜色</p>
<p>示例如下：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> torchvision <span class="keyword">import</span> transforms</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> torchvision.datasets <span class="keyword">import</span> MNIST</span><br><span class="line">mnist=MNIST(root=<span class="string">&quot;./data&quot;</span>,train=<span class="literal">True</span>,download=<span class="literal">False</span>)</span><br><span class="line">ret=transforms.ToTensor()(mnist[<span class="number">0</span>][<span class="number">0</span>])<span class="comment">#这里用到了transforms.ToTensor的__call___方法，可以直接对其实例传入数据获取结果</span></span><br><span class="line"><span class="built_in">print</span>(ret.size())</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<pre><code>torch.Size([1, 28, 28])</code></pre>
<p><code>torchvision.transforms.Normalize(mean, std)</code></p>
<p>给定均值：mean，shape和图片的通道数相同(指的是每个通道的均值)，方差：std，和图片的通道数相同(指的是每个通道的方差)，将会把Tensor规范化处理。
+ Normalize中并没有帮我们计算，所以我们需要手动计算</p>
<ul>
<li>当mean为全部数据的均值，std为全部数据的std的时候，才是进行了标准化。</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> torchvision <span class="keyword">import</span> transforms</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"></span><br><span class="line">data = np.random.randint(<span class="number">0</span>, <span class="number">255</span>, size=<span class="number">12</span>)</span><br><span class="line">img = data.reshape(<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">img = transforms.ToTensor()(img) <span class="comment"># 转换成tensor</span></span><br><span class="line"><span class="built_in">print</span>(img)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*&quot;</span>*<span class="number">100</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#关键一步，因为transforms,Normarlize 不支持整形，支持浮点型，所以得先转化为浮点型</span></span><br><span class="line">img = img.<span class="built_in">float</span>()</span><br><span class="line"></span><br><span class="line">norm_img= transforms.Normalize((<span class="number">10</span>,<span class="number">10</span>,<span class="number">10</span>),(<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>))(img) <span class="comment">#进行规范化处理</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(norm_img)</span><br></pre></td></tr></table></figure>
<pre><code>tensor([[[ 48,  13],
         [ 95,  37]],

        [[171,  97],
         [210, 184]],

        [[ 74, 123],
         [240, 147]]], dtype=torch.int32)
****************************************************************************************************
tensor([[[ 38.,   3.],
         [ 85.,  27.]],

        [[161.,  87.],
         [200., 174.]],

        [[ 64., 113.],
         [230., 137.]]])</code></pre>
<p><code>torchvision.transforms.Compose(transforms)</code></p>
<p>将多个transform组合起来使用</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> torchvision <span class="keyword">import</span> transforms</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"></span><br><span class="line">data = np.random.randint(<span class="number">0</span>, <span class="number">255</span>, size=<span class="number">12</span>)</span><br><span class="line">img = data.reshape(<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line"><span class="comment">#先把np改为浮点型</span></span><br><span class="line">img = img.astype(np.float32)</span><br><span class="line"></span><br><span class="line">transforms.Compose([</span><br><span class="line">     torchvision.transforms.ToTensor(), <span class="comment">#先转化为Tensor</span></span><br><span class="line">     torchvision.transforms.Normalize(<span class="number">1</span>,<span class="number">1</span>) <span class="comment">#在进行正则化</span></span><br><span class="line"> ])(img)</span><br></pre></td></tr></table></figure>
<pre><code>[[[154  65  36]
  [141 199 183]]

 [[219 125 148]
  [166  63 217]]]





tensor([[[153., 140.],
         [218., 165.]],

        [[ 64., 198.],
         [124.,  62.]],

        [[ 35., 182.],
         [147., 216.]]])</code></pre>
<h4 id="自己做的">自己做的</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> torchvision.transforms <span class="keyword">import</span> Compose,ToTensor,Normalize</span><br><span class="line"><span class="keyword">from</span> torchvision <span class="keyword">import</span> transforms</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> Dataset, DataLoader</span><br><span class="line"><span class="keyword">from</span> torchvision.datasets <span class="keyword">import</span> MNIST</span><br><span class="line"><span class="keyword">import</span> torch.nn.functional <span class="keyword">as</span> F </span><br><span class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.optim <span class="keyword">import</span> Adam</span><br><span class="line"></span><br><span class="line"><span class="comment">#数据加载</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_dataloader</span>(<span class="params">train=<span class="literal">True</span></span>):</span><br><span class="line">    transform_fn=Compose([</span><br><span class="line">    ToTensor(),</span><br><span class="line">    Normalize(mean=(<span class="number">0.1307</span>,),std=(<span class="number">0.3081</span>))</span><br><span class="line">        ])</span><br><span class="line">    dataset=MNIST(root=<span class="string">&quot;./data&quot;</span>,train=train,download=<span class="literal">False</span>,transform=transform_fn)</span><br><span class="line">    data_loader=DataLoader(dataset,batch_size=<span class="number">256</span>,shuffle=<span class="literal">True</span>)</span><br><span class="line">    <span class="keyword">return</span> data_loader</span><br><span class="line"></span><br><span class="line"><span class="comment">#神经网络构建</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MnistModel</span>(nn.Module):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line">        self.fc1=nn.Linear(<span class="number">1</span>*<span class="number">28</span>*<span class="number">28</span>,<span class="number">28</span>)</span><br><span class="line">        self.fc2=nn.Linear(<span class="number">28</span>,<span class="number">10</span>)<span class="comment">#定义Linear的输入输出形状</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self,x</span>):</span><br><span class="line">        x=x.view(-<span class="number">1</span>,<span class="number">1</span>*<span class="number">28</span>*<span class="number">28</span>)</span><br><span class="line">        x=self.fc1(x)</span><br><span class="line">        x=F.relu(x)<span class="comment">#激活函数处理</span></span><br><span class="line">        out=self.fc2(x)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> out</span><br><span class="line"></span><br><span class="line"><span class="comment">#实例化对象</span></span><br><span class="line"></span><br><span class="line">model=MnistModel()</span><br><span class="line">optimizer=Adam(model.parameters(),lr=<span class="number">0.001</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>(<span class="params">epoch</span>):</span><br><span class="line">    data_loader=get_dataloader() <span class="comment">#这个不能放在train函数外面</span></span><br><span class="line">    criterion=nn.CrossEntropyLoss()  <span class="comment">#实例化损失</span></span><br><span class="line">    <span class="keyword">for</span> i,(<span class="built_in">input</span>,target) <span class="keyword">in</span> <span class="built_in">enumerate</span>(data_loader):</span><br><span class="line">        optimizer.zero_grad()</span><br><span class="line">        output=model(<span class="built_in">input</span>) <span class="comment">#调用模型得到预测值1</span></span><br><span class="line">        loss=criterion(output,target)</span><br><span class="line">        loss.backward()  <span class="comment">#反向传播</span></span><br><span class="line">        optimizer.step() <span class="comment">#梯度更新</span></span><br><span class="line">        <span class="keyword">if</span> i%<span class="number">100</span>==<span class="number">0</span>:</span><br><span class="line">            <span class="built_in">print</span>(loss.item())</span><br><span class="line">        </span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">    train(i)</span><br><span class="line">torch.save(mnist_net.state_dict(),<span class="string">&quot;smnist_net.pt&quot;</span>)  <span class="comment"># 保存模型参数</span></span><br><span class="line">torch.save(optimizer.state_dict(),<span class="string">&quot;smnist_opt.pt&quot;</span>)  <span class="comment"># 保存优化器参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    </span><br></pre></td></tr></table></figure>
<pre><code>2.324977397918701
0.332792192697525
0.2844235897064209
0.2815473973751068
0.32116132974624634
0.21920350193977356
0.26661691069602966
0.2610546350479126
0.312256395816803</code></pre>
<h4 id="结合别人做的">结合别人做的</h4>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># -*- coding:utf-8 -*-</span></span><br><span class="line"><span class="comment"># author: Anefuer_kpl</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># project: Pytorch学习</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> Dataset, DataLoader</span><br><span class="line"><span class="keyword">from</span> torchvision.datasets <span class="keyword">import</span> MNIST</span><br><span class="line"><span class="keyword">from</span> torchvision <span class="keyword">import</span> transforms</span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">import</span> torch.nn.functional <span class="keyword">as</span> F</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> optim</span><br><span class="line"></span><br><span class="line">BATCH_SIZE = <span class="number">256</span></span><br><span class="line">EPOCH = <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">    切记：torch中处理图片需要将shape `(H,W,C)` 改为 `(C,H,W)`, 也就是将通道数放到最前面</span></span><br><span class="line"><span class="string">         使用 `torchvision.transforms.ToTensor` 进行转换</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1.准备数据集</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_dataloader</span>(<span class="params">train=<span class="literal">True</span></span>):</span><br><span class="line">    transform_fn = transforms.Compose([</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(<span class="number">0.1307</span>, <span class="number">0.3081</span>)  <span class="comment"># mean 和 std</span></span><br><span class="line">    ])</span><br><span class="line">    mnist = MNIST(root=<span class="string">&#x27;./data&#x27;</span>,</span><br><span class="line">                  train=train,</span><br><span class="line">                  transform=transform_fn)  <span class="comment"># PS: 如果root目录下有数据，那么就不会再下载了</span></span><br><span class="line"></span><br><span class="line">    data_loader = DataLoader(dataset=mnist, batch_size=BATCH_SIZE, shuffle=<span class="literal">True</span>)</span><br><span class="line">    <span class="keyword">return</span> data_loader, <span class="built_in">len</span>(mnist)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.构建模型网络</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MnistModel</span>(nn.Module):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line">        self.fc1 = nn.Linear(<span class="number">1</span> * <span class="number">28</span> * <span class="number">28</span>, <span class="number">28</span>)</span><br><span class="line">        self.fc2 = nn.Linear(<span class="number">28</span>, <span class="number">10</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self, <span class="built_in">input</span></span>):</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        :param input: [batch_size, 1, 28, 28]</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">        &#x27;&#x27;&#x27;</span></span><br><span class="line">        <span class="comment"># 1.修改形状</span></span><br><span class="line">        x = <span class="built_in">input</span>.view([-<span class="number">1</span>, <span class="number">1</span> * <span class="number">28</span> * <span class="number">28</span>])</span><br><span class="line">        <span class="comment"># 2.进行全连接操作</span></span><br><span class="line">        x = self.fc1(x)</span><br><span class="line">        <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">          3.激活函数处理，形状不会发生变化</span></span><br><span class="line"><span class="string">        &#x27;&#x27;&#x27;</span></span><br><span class="line">        x = F.relu(x)</span><br><span class="line">        <span class="comment"># 4.再次进行全连接操作</span></span><br><span class="line">        out = self.fc2(x)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> F.log_softmax(out,dim=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">mnist_net = MnistModel()</span><br><span class="line">optimizer = optim.Adam(mnist_net.parameters(), lr=<span class="number">1e-3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3.进行训练</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>(<span class="params">epoch</span>):</span><br><span class="line">    data_loader, length = get_dataloader()</span><br><span class="line">    <span class="keyword">for</span> index, (<span class="built_in">input</span>, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(data_loader):</span><br><span class="line">        optimizer.zero_grad()</span><br><span class="line">        output = mnist_net(<span class="built_in">input</span>)  <span class="comment"># 调用模型，得到预测值</span></span><br><span class="line">        loss = F.nll_loss(output, label)  <span class="comment"># 带权的交叉熵损失</span></span><br><span class="line">        loss.backward()  <span class="comment"># 反向传播</span></span><br><span class="line">        optimizer.step()  <span class="comment"># 更新参数</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> index % <span class="number">100</span> == <span class="number">0</span>:</span><br><span class="line">            <span class="comment"># 4.模型保存</span></span><br><span class="line">            <span class="comment"># 每隔100次保存一次</span></span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&#x27;epoch(<span class="subst">&#123;epoch&#125;</span>) loss:&#x27;</span>, loss.item())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(EPOCH):</span><br><span class="line">    train(i)</span><br><span class="line">    torch.save(mnist_net.state_dict(),<span class="string">f&quot;selfmod/mnist_net<span class="subst">&#123;i&#125;</span>.pt&quot;</span>)  <span class="comment"># 保存模型参数</span></span><br><span class="line">    torch.save(optimizer.state_dict(),<span class="string">f&quot;selfmod/mnist_opt<span class="subst">&#123;i&#125;</span>.pt&quot;</span>)  <span class="comment"># 保存优化器参数</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<pre><code>Test set: Avg. loss: 0.1988, acc:9425/10000 94.25%</code></pre>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">test</span>():</span><br><span class="line">    test_loss = <span class="number">0</span></span><br><span class="line">    acc = <span class="number">0</span></span><br><span class="line">    mnist_net.<span class="built_in">eval</span>()  <span class="comment"># 设置模型为评估模式</span></span><br><span class="line">    test_dataloader, length = get_dataloader(train=<span class="literal">False</span>)</span><br><span class="line">    <span class="keyword">with</span> torch.no_grad(): <span class="comment"># 不计算梯度</span></span><br><span class="line">        <span class="keyword">for</span> <span class="built_in">input</span>, label <span class="keyword">in</span> test_dataloader:</span><br><span class="line">            output = mnist_net(<span class="built_in">input</span>)</span><br><span class="line">            <span class="comment"># print(&#x27;output&#x27;, output)</span></span><br><span class="line">            test_loss += F.nll_loss(output, label, reduction=<span class="string">&#x27;sum&#x27;</span>).item()</span><br><span class="line">            <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">                损失函数nll_loss讲解：https://www.gentlecp.com/articles/874.html </span></span><br><span class="line"><span class="string">                reduction=&#x27;sum&#x27; 表示对一个batch的loss求和， 而 test_loss+= 表示对所有batch的loss进行求和</span></span><br><span class="line"><span class="string">            &#x27;&#x27;&#x27;</span></span><br><span class="line">            predict = output.data.<span class="built_in">max</span>(<span class="number">1</span>, keepdim=<span class="literal">True</span>)[<span class="number">1</span>]</span><br><span class="line">            <span class="comment"># print(&#x27;predict&#x27;, predict)</span></span><br><span class="line">            <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">                找到输出中 概率最大的那一项 对应的索引（因为索引正好对应着标签）， </span></span><br><span class="line"><span class="string">                keepdim=True表示保留最大值对应的索引</span></span><br><span class="line"><span class="string">            &#x27;&#x27;&#x27;</span></span><br><span class="line">            acc += predict.eq(label.data.view_as(predict)).<span class="built_in">sum</span>()  <span class="comment"># 对预测正确的个数累加</span></span><br><span class="line">            <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">                view_as返回被视作与给定的tensor相同大小的原tensor</span></span><br><span class="line"><span class="string">                一个label中包含了一个batch中所有的标签，view_as把label变成和predict一样的形状</span></span><br><span class="line"><span class="string">                使用 eq 对两个tensor中的值进行比较，如：</span></span><br><span class="line"><span class="string">                    predict     [0, 3, 4, 1]</span></span><br><span class="line"><span class="string">                    label       [2, 3, 4, 2]</span></span><br><span class="line"><span class="string">                返回值是bool列表，然后用sum对bool列表进行求和，从而计算出该batch中有多少个是预测正确的</span></span><br><span class="line"><span class="string">                acc+= 表示对所有的batch预测正确的个数进行求和</span></span><br><span class="line"><span class="string">            &#x27;&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line">    test_loss /= length  <span class="comment"># 计算平均损失, length 为mnist数据集的数量</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;Test set: Avg. loss: &#123;:.4f&#125;, acc:&#123;&#125;/&#123;&#125; &#123;:.2f&#125;%&#x27;</span>.<span class="built_in">format</span>(</span><br><span class="line">        test_loss, acc, length, <span class="number">100.</span>*acc/length</span><br><span class="line">    ))</span><br><span class="line"></span><br><span class="line"><span class="comment">#     加载训练好的模型</span></span><br><span class="line">mnist_net.load_state_dict(torch.load(<span class="string">&#x27;selfmod/mnist_net2.pt&#x27;</span>))</span><br><span class="line">optimizer.load_state_dict(torch.load(<span class="string">&#x27;selfmod/mnist_opt2.pt&#x27;</span>))</span><br><span class="line">test()</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io">杨一赫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io/2022/10/05/pytorch%E5%AE%9E%E7%8E%B0%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB/">http://yang1he.gitee.io/2022/10/05/pytorch%E5%AE%9E%E7%8E%B0%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yang1he.gitee.io" target="_blank">杨一赫的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%8A%A8%E6%89%8B%E5%AD%A6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">-动手学深度学习</a></div><div class="post_share"><div class="social-share" data-image="/img/rick1.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/11/23/GNN%E7%BB%BC%E8%BF%B0%E7%A0%94%E8%AF%BB/"><img class="prev-cover" src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/v2-a489a8f84b998439d6e681dc07a13080_720w.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">GNN综述研读</div></div></a></div><div class="next-post pull-right"><a href="/2022/09/15/A%20Multi-Layer%20Fusion%20Neural%20Network/"><img class="next-cover" src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20220912135851165.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">A Multi-Layer Fusion Neural Network</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon2.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">杨一赫</div><div class="author-info__description">阳光开朗大男孩</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/yang1he"><i class="fab fa-github"></i><span>gitee</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">平平无奇的网站</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#chapter1torch-%E5%85%A5%E9%97%A8"><span class="toc-number">1.</span> <span class="toc-text">chapter1,torch 入门</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%BC%A0%E9%87%8F"><span class="toc-number">1.1.</span> <span class="toc-text">创建张量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">常用方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.</span> <span class="toc-text">数据类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#tensor%E7%9A%84%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C"><span class="toc-number">1.4.</span> <span class="toc-text">tensor的其他操作</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#chapter2-%E6%A2%AF%E5%BA%A6%E4%B8%8B%E9%99%8D%E4%B8%8E%E5%8F%8D%E5%90%91%E4%BC%A0%E6%92%AD"><span class="toc-number">2.</span> <span class="toc-text">chapter2 梯度下降与反向传播</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">3.</span> <span class="toc-text">线性回归的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%8B%E6%95%B2"><span class="toc-number">3.1.</span> <span class="toc-text">手敲</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%80%9F%E5%8A%A9api"><span class="toc-number">3.2.</span> <span class="toc-text">借助api</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8gpu%E4%B8%8A%E8%BF%90%E8%A1%8C%E4%BB%A3%E7%A0%81"><span class="toc-number">3.3.</span> <span class="toc-text">在GPu上运行代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E4%BC%98%E5%8C%96%E7%AE%97%E6%B3%95%E4%BB%8B%E7%BB%8D"><span class="toc-number">3.4.</span> <span class="toc-text">常见优化算法介绍</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%8A%A0%E8%BD%BD"><span class="toc-number">4.</span> <span class="toc-text">数据加载</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#dataset%E4%BB%8B%E7%BB%8D"><span class="toc-number">4.1.</span> <span class="toc-text">DATASET介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dataloader%E7%B1%BB"><span class="toc-number">4.2.</span> <span class="toc-text">DATALOADER类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#pytorch%E8%87%AA%E5%B8%A6%E6%95%B0%E6%8D%AE%E9%9B%86"><span class="toc-number">4.3.</span> <span class="toc-text">pytorch自带数据集</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9B%E8%A1%8C%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E8%AF%86%E5%88%AB"><span class="toc-number">5.</span> <span class="toc-text">进行手写数字识别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#image%E5%A4%84%E7%90%86"><span class="toc-number">5.1.</span> <span class="toc-text">image处理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#torchvision.transforms%E7%9A%84%E5%9B%BE%E5%BD%A2%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E6%96%B9%E6%B3%95"><span class="toc-number">5.1.1.</span> <span class="toc-text">torchvision.transforms的图形数据处理方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%87%AA%E5%B7%B1%E5%81%9A%E7%9A%84"><span class="toc-number">5.1.2.</span> <span class="toc-text">自己做的</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%93%E5%90%88%E5%88%AB%E4%BA%BA%E5%81%9A%E7%9A%84"><span class="toc-number">5.1.3.</span> <span class="toc-text">结合别人做的</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/model_00.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"/></a><div class="content"><a class="title" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks">An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks</a><time datetime="2023-06-23T16:00:00.000Z" title="发表于 2023-06-24 00:00:00">2023-06-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题"><img src="/img/rick1.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题">无题</a><time datetime="2023-03-05T05:57:17.050Z" title="发表于 2023-03-05 13:57:17">2023-03-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230305093941469.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"/></a><div class="content"><a class="title" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能">三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能</a><time datetime="2023-03-03T16:00:00.000Z" title="发表于 2023-03-04 00:00:00">2023-03-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"/></a><div class="content"><a class="title" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</a><time datetime="2023-03-02T16:00:00.000Z" title="发表于 2023-03-03 00:00:00">2023-03-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230228145723183.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="GPTEX---为ChatGPT而生的latex软件"/></a><div class="content"><a class="title" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件">GPTEX---为ChatGPT而生的latex软件</a><time datetime="2023-02-27T16:00:00.000Z" title="发表于 2023-02-28 00:00:00">2023-02-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By 杨一赫</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>function addGitalkSource () {
  const ele = document.createElement('link')
  ele.rel = 'stylesheet'
  ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
  document.getElementsByTagName('head')[0].appendChild(ele)
}

function loadGitalk () {
  function initGitalk () {
    var gitalk = new Gitalk(Object.assign({
      clientID: '5a73aec03d8a0aeb971f',
      clientSecret: '54ad1c8adab95b382d582c73e9f597781de43525',
      repo: 'comment',
      owner: 'nmhjklnm',
      admin: ['nmhjklnm'],
      id: '931cfda0e11f114b23664bc604524576',
      updateCountCallback: commentCount
    },null))

    gitalk.render('gitalk-container')
  }

  if (typeof Gitalk === 'function') initGitalk()
  else {
    addGitalkSource()
    getScript('https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js').then(initGitalk)
  }
}

function commentCount(n){
  let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
  if (isCommentCount) {
    isCommentCount.innerHTML= n
  }
}

if ('Gitalk' === 'Gitalk' || !false) {
  if (false) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
  else loadGitalk()
} else {
  function loadOtherComment () {
    loadGitalk()
  }
}</script></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="//code.tidio.co/6zvu6bgfljrvvv9rw1y7hzehoriklr03.js" async="async"></script><script>function onTidioChatApiReady() {
  window.tidioChatApi.hide();
  window.tidioChatApi.on("close", function() {
    window.tidioChatApi.hide();
  });
}
if (window.tidioChatApi) {
  window.tidioChatApi.on("ready", onTidioChatApiReady);
} else {
  document.addEventListener("tidioChat-ready", onTidioChatApiReady);
}

var chatBtnFn = () => {
  document.getElementById("chat_btn").addEventListener("click", function(){
    window.tidioChatApi.show();
    window.tidioChatApi.open();
  });
}
chatBtnFn()
</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>